﻿
Imports osi.root.constants
Imports osi.root.template
Imports osi.root.connector

Public Class fixed_pool(Of t)
    Inherits fixed_pool(Of t, _true, _false)

    Public Sub New(Optional ByVal buffsize As Int64 = default_fixed_pool_length)
        MyBase.New(buffsize)
    End Sub
End Class

Public Class fixed_pool(Of t, _THREADSAFE As _boolean, _RNDSELECT As _boolean)
    Inherits pool(Of t, _THREADSAFE, _RNDSELECT, _false)

    Private ReadOnly _pool() As t
    Private ReadOnly _free() As Boolean

    Public Sub New(Optional ByVal buffsize As Int64 = default_fixed_pool_length)
        MyBase.New()
        If buffsize < 0 Then
            buffsize = default_fixed_pool_length
        End If

        ReDim _pool(buffsize - 1)
        ReDim _free(buffsize - 1)
        memset(_free, 0, buffsize, True)
    End Sub

    Protected Overrides Property at(ByVal index As Int64) As t
        Get
            Return _pool(index)
        End Get
        Set(ByVal value As t)
            _pool(index) = value
        End Set
    End Property

    Protected Overrides Property free(ByVal index As Int64) As Boolean
        Get
            Return _free(index)
        End Get
        Set(ByVal value As Boolean)
            k_assert(Not _free(index))
            _free(index) = value
        End Set
    End Property

    Protected Overrides Function size() As Int64
        Return _pool.Length()
    End Function

    Protected Overrides Sub Finalize()
        GC.KeepAlive(_pool)
        GC.KeepAlive(_free)
        MyBase.Finalize()
    End Sub
End Class
